package April._25.answer.work3;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class Main {
    public void callTargetMethod(String configFilePath) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        // 加载配置文件
        Properties properties = loadConfigFile(configFilePath);
        // 读取配置信息
        String className = properties.getProperty("className");
        String methodName = properties.getProperty("methodName");

        // 获取字节码文件对象
        Class targetClass = Class.forName(className);
        // 获取method对象
        Method declaredMethod = targetClass.getDeclaredMethod(methodName);

        // 实例化对象
        Object targetObje = targetClass.newInstance();
        // 调用方法---重点
        declaredMethod.invoke(targetObje);
    }

    private Properties loadConfigFile(String configFilePath) throws IOException {
        Properties properties = new Properties();
        properties.load(new FileInputStream(configFilePath));
        return properties;
    }
}
